/*
 * Copyright © 2023 广州宏天软件股份有限公司 hotent
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hotent.project.auth.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hotent.project.auth.service.AuthenticationService;
import org.apache.http.client.ClientProtocolException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.exception.CertificateException;
import com.hotent.base.jwt.JwtAuthenticationRequest;
import com.hotent.base.model.CommonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/auth/v1")
@Api(tags = "auth")
@ApiGroup(group = {"defaultApi"})
public class AuthController {
    @Resource
    AuthenticationService authenticationService;

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "登录系统", httpMethod = "POST", notes = "登录系统")
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
        throws AuthenticationException, CertificateException, InvalidKeySpecException, NoSuchAlgorithmException {
        return authenticationService.createAuthenticationToken(authenticationRequest);
    }

    @RequestMapping(value = "/refresh", method = RequestMethod.GET)
    @ApiOperation(value = "刷新token", httpMethod = "GET", notes = "刷新token")
    public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest request) {
        return authenticationService.refreshAndGetAuthenticationToken(request);
    }

    @RequestMapping(value = "/signout", method = RequestMethod.GET)
    @ApiOperation(value = "退出登录", httpMethod = "GET", notes = "使token的状态失效,必须设置jwt.single和jwt.stricky均为true")
    public CommonResult<String> signout(HttpServletRequest request) {
        authenticationService.signOut(request);
        return CommonResult.<String>ok().message("退出成功");
    }

    @RequestMapping(value = "/sso/auth", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "登录系统-单点登录", httpMethod = "GET", notes = "登录系统-单点登录")
    public ResponseEntity<?> ssoAuth(@RequestParam Optional<String> ticket, @RequestParam Optional<String> code, @RequestParam Optional<String> ssoMode, @RequestParam Optional<String> ssoUserId,
        @RequestParam String service) throws AuthenticationException, ClientProtocolException, IOException {
        return authenticationService.ssoAuth(ticket, code, ssoMode, ssoUserId, service);
    }
}
